var username=localStorage.getItem("username");
var token=localStorage.getItem("token");
var userid=localStorage.getItem("userid");
$(function(){
	gridObj = $.fn.bsgrid.init('g_tabYeh', {
		ajaxType : 'get',
		url :  server_url+'/rest/supplyManage/supplyList.do?ran=' + Math.random() + '&token=' + token,
		autoLoad : true,
		pageSizeSelect : true,
		stripeRows : true,
		pageSize : 10,
		displayBlankRows : false,
		isProcessLockScreen : true,
		rowSelectedColor : false,
		showPageToolbar:true,
		rowHoverColor:true
	});	
	
});
function operate(record, rowIndex, colIndex, options) {
    var re = '<a href="javascript:editSupply(\'' + gridObj.getRecordIndexValue(record, 'supplyID') + '\');">修改</a>'
    +'<a href="javascript:viewSupply(\'' + gridObj.getRecordIndexValue(record, 'supplyID') + '\');">查看</a>';
    if(record.supplyStatus != 4){
    	re+= '<a href="javascript:frozenSupply(\'' + gridObj.getRecordIndexValue(record, 'supplyID') + '\');">冻结</a>';
    }
    return re;
}
function toFrozenSupply(id) {
	var url='supplyManage/frozenSupply.do?id='+id;
	tl_config.loadToWorkArea(url);
}
// 数据表格参数查询
function doSearch() {
	searchParames = $('#myform').serialize();
    gridObj.search(searchParames);		
	//console.log(searchParames);
}

// 修改
function editSupply(id) {
	var url='supplyManage/editSupply.do?id='+id+'&random='+Math.random();
	tl_config.loadToWorkArea(url);
}
function viewSupply(id){
	var url='supplyManage/viewSupply.do?id='+id+'&random='+Math.random();
	tl_config.loadToWorkArea(url);
}

function getCheckedIds() {
	// values are array
	return gridObj.getCheckedValues('supplyID');
}
function delCheckedIds(){
	var idArr=getCheckedIds();
	var ids=idArr.toString();
	console.log(ids);
	if(empty(ids)){
		easyDialog.open({
			  container : {
				  header : '温馨提示',
			    content : '请选择要操作的记录！'
			  },
			  autoClose : 2000
			});
		return;
	}
	easyDialog.open({
		  container : {
			  header : '删除确认',
			  content : '您确定要删除这些供应商么？',
			  yesFn : function(){
		    	$.ajax({
					url:server_url+'/rest/supplyManage/deleteSupply.do?ran='+Math.random()+"&token="+token,
					type:'get',
					data:{supplyids:ids},
					success:function(data){
						if(data.retCode=="0000"){
							doSearch();
						}
						else{
							easyDialog.open({
								  container : {
									  header : '提示',
								    content : data.retMsg
								  },
								  autoClose : 2000
								});
							
						} 
						
					}
				})
		    },
		    noFn : true
		  }
		});
}

function frozenSupply(supplyId){

	easyDialog.open({
		container : {
			header : '冻结确认',
			content : '您确定要冻结这个供应商么？',
			yesFn : function(){
				$.ajax({
					url:server_url+'/rest/supplyManage/checkFrozenSupply/'+supplyId+'.do?ran='+Math.random()+"&token="+token,
					type:'get',
					data:{operator:username},
					success:function(data){
						if(data.retCode=="0000"){
//							doSearch();
							easyDialog.open({
								container : {
									header : '提示',
									content : '冻结成功',
									yesFn:function(){
										var url=server_url+'/supplyManage/list.do';
										tl_config.last_url=url;
										tl_config.loadToWorkArea(url);
									}
								}
							});
						}
						else{
							easyDialog.open({
								container : {
									header : '提示',
									content : data.retMsg
								},
								autoClose : 2000
							});

						}

					}
				})
			},
			noFn : true
		}
	});
}